/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.bo.PracticeRecordBO;
import com.xy.biz.ts.domain.req.UpdatePracticeRecordRequest;
import com.xy.biz.ts.domain.resp.GetTrainByClassifyResponse;
import com.xy.biz.ts.service.PracticeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.resp.GetPracticeRecordResponse;
import com.xy.biz.ts.domain.req.GetPracticeRecordRequest;
import com.xy.biz.ts.domain.req.ListPracticeRecordRequest;
import com.xy.biz.ts.domain.req.SavePracticeRecordRequest;
import com.xy.biz.ts.domain.query.PracticeRecordQuery;
import com.xy.biz.ts.domain.converter.PracticeRecordConverter;
import com.xy.biz.ts.client.PracticeRecordClient;

/**
 *
 *
 * @author wwj
 * @date 2021-4-7
 */
@Api(tags = "实操记录")
@Slf4j
@RequestMapping("/api/ts/practice_record")
@RestController
public class PracticeRecordController implements PracticeRecordClient {

    @Autowired
    private PracticeRecordService practiceRecordService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> savePracticeRecord(@RequestBody SavePracticeRecordRequest request) {
        return practiceRecordService.savePracticeRecord(PracticeRecordConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllPracticeRecord(@RequestBody Collection<SavePracticeRecordRequest> collection) {
        practiceRecordService.saveAllPracticeRecord(PracticeRecordConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updatePracticeRecord(@RequestBody UpdatePracticeRecordRequest request) {
        return practiceRecordService.updatePracticeRecord(PracticeRecordConverter.toBO(request));
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deletePracticeRecord(@RequestBody GetPracticeRecordRequest request) {
        practiceRecordService.deletePracticeRecord(request.getPracticeRecordId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<?> getPracticeRecord(@RequestBody GetPracticeRecordRequest request) {
        return practiceRecordService.getPracticeRecord(request.getPracticeRecordId());
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetPracticeRecordResponse>> listPracticeRecordById(@RequestBody Collection<Long> collection) {
        return Result.success(PracticeRecordConverter.toResponse(practiceRecordService.listPracticeRecordById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetPracticeRecordResponse>> listPracticeRecord(@RequestBody ListPracticeRecordRequest request) {
        return Result.success(PracticeRecordConverter.toResponse(practiceRecordService.listPracticeRecord(PracticeRecordConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countPracticeRecord(@RequestBody ListPracticeRecordRequest request) {
        return Result.success(practiceRecordService.countPracticeRecord(PracticeRecordConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetPracticeRecordResponse>> pagePracticeRecord(@RequestBody ListPracticeRecordRequest request) {
        PracticeRecordQuery query = PracticeRecordConverter.toQuery(request);
        List<GetPracticeRecordResponse> list = PracticeRecordConverter.toResponse(practiceRecordService.listPracticeRecord(query));
        int count = practiceRecordService.countPracticeRecord(query);
        return Result.success(new Page<>(list, count));
    }

}
